package ntd.util.frame.action.zzfm.fk.hk;

import cn.hutool.core.util.StrUtil;
import cn.hutool.http.HttpUtil;
import cn.hutool.json.JSONArray;
import cn.hutool.json.JSONObject;
import cn.hutool.json.JSONUtil;
import ntd.util.cache.ComCache;
import ntd.util.cache.FactoryCache;
import ntd.util.config.common.exception.ComException;

import java.util.HashMap;
import java.util.Map;

/**
 * 海控科技远程操作
 * 平台地址：http://hk.tchjjc.com
 * 平台账号：YH18637363843
 * 平台密码：18637363843
 * 设备名称：
 * H4GSAL1132202262208
 * H4GSAL1132202261137
 * H4GSAL1132202262223
 * H4GSAL1132202261175
 * H4GSAL1132202262202
 */
public class RemoteHk {

    /** 平台api接入地址 */
    private String BASE_API = "http://hkapi.tchjjc.com";

    /** 平台api接入账号 */
    private String ACCOUNT = "YH18637363843";

    /** 平台api接入密码 */
    private String PASSWORD = "18637363843";

    /** 产品的key */
    private String PK = "a1Wvw3VrwFk";

    /** token 缓存 */
    private final ComCache cacheToken = FactoryCache.cache("remote-hk-token", 70000);

    public RemoteHk(String api, String key, String pass, String pk) {
        this.BASE_API = api;
        this.ACCOUNT = key;
        this.PASSWORD = pass;
        this.PK = pk;
    }

    /**
     * 初始化平台接入信息
     */
    public String token() {
        return cacheToken.cache(() -> {
            Map<String, Object> params =  new HashMap<>();
            params.put("username", ACCOUNT);
            params.put("pwd", PASSWORD);
            String res = HttpUtil.post(BASE_API + "/api/token", params);
            JSONObject resJson = JSONUtil.parseObj(res);

            if (resJson.getInt("code") != 200) {
                throw new ComException("服务器token获取失败");
            }

            return resJson.getStr("token");
        });
    }

    public int getStatus(String code) {
        Map<String, Object> params =  new HashMap<>();
        params.put("token", token());
        params.put("pk", PK);
        params.put("deviceName", code);
        String res = HttpUtil.post(BASE_API + "/api/thing/status", params);
        JSONObject resJson = JSONUtil.parseObj(res);

        if (resJson.getInt("code") != 200) {
            throw new ComException("获取设备状态失败");
        }

        return resJson.getByPath("data.status", Integer.class);
    }

    /**
     * 设置设备属性
     * @param code 设备编码
     * @param key 属性名称
     * @param val 属性值
     * @return
     */
    public boolean setProperty(String code, String key, String val) {
        Map<String, Object> params =  new HashMap<>();
        params.put("token", token());
        params.put("pk", PK);
        params.put("deviceName", code);
        params.put("identifier", key);
        params.put("value", val);
        String res = HttpUtil.post(BASE_API + "/api/thing/properties/set", params);
        JSONObject resJson = JSONUtil.parseObj(res);
        return resJson.getInt("code") == 200;
    }

    public String getProperty(String code, String key) {
        Map<String, Object> params =  new HashMap<>();
        params.put("token", token());
        params.put("pk", PK);
        params.put("deviceName", code);
        String res = HttpUtil.post(BASE_API + "/api/thing/properties", params);

        JSONObject resJson = JSONUtil.parseObj(res);

        if (resJson.getInt("code") != 200) {
            throw new ComException("获取设备状态失败");
        }

        JSONArray data = resJson.getJSONArray("data");
        for (int i = 0; i < data.size(); i++) {
            JSONObject item = data.getJSONObject(i);
            if (StrUtil.equals(key, item.getStr("attribute"))) {
                return item.getStr("value");
            }
        }

        throw new ComException("未找到该属性");
    }

    public static void main(String[] args) {
//        RemoteHk remoteHk = new RemoteHk();
//        StaticLog.info("" + remoteHk.getStatus("H4GSAL1132202262208"));
//        StaticLog.info(remoteHk.getProperty("H4GSAL1132202262208", "rly1"));
    }


}
